package com.minimalist.tc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.tc.entity.enums.TcOrderEnum;
import com.minimalist.common.entity.tc.entity.excel.TcOrderExcelData;
import com.minimalist.common.entity.tc.entity.po.TcOrder;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderQueryVO;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.tc.manager.TcManager;
import com.minimalist.tc.mapper.TcOrderMapper;
import com.minimalist.tc.service.TcOrderService;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 停车订单表 服务层实现。
 *
 * @author 11987
 * @since 2025-04-07
 */
@Service
public class TcOrderServiceImpl extends ServiceImpl<TcOrderMapper, TcOrder>
        implements TcOrderService{
    @Autowired
    private TcOrderMapper mapper;

    @Autowired
    private TcManager tcManager;

    @Override
    public void addOrder(TcOrderVO orderVO){
        TcOrder tcOrder = new TcOrder();
        BeanUtil.copyProperties(orderVO, tcOrder);
        tcOrder.setOrderId(UnqIdUtil.uniqueId());
        mapper.insert(tcOrder);
    }

    @Override
    public void deleteOrderByOrderId(Long orderId){
        Assert.notNull(mapper.selectOrderByOrderId(orderId),
                () -> new BusinessException(
                        TcOrderEnum.ErrorMsgEnum.ORDER_NOT_EXIST.getDesc()));
        mapper.deleteOrderByOrderId(orderId);
    }

    @Override
    public void updateOrderByOrderId(TcOrderVO orderVO){
        TcOrder tcOrder = mapper.selectOrderByOrderId(orderVO.getOrderId());
        Assert.notNull(tcOrder, () -> new BusinessException(
                TcOrderEnum.ErrorMsgEnum.ORDER_NOT_EXIST.getDesc()));
        BeanUtil.copyProperties(orderVO, tcOrder);
        mapper.updateOrderByOrderId(tcOrder);
    }

    @Override
    public PageResp<TcOrderVO> getPageOrderList(TcOrderQueryVO queryVO){
        Page<TcOrder> page = mapper.selectPageOrderList(queryVO);
        if (page.hasRecords()){
            List<TcOrderVO> list = BeanUtil.copyToList(page.getRecords(),
                    TcOrderVO.class);
            return new PageResp<>(list, page.getTotalRow());
        }
        return null;
    }

    @Override
    public TcOrderVO getOrderByOrderId(Long orderId){
        TcOrder tcOrder = mapper.selectOrderByOrderId(orderId);
        Assert.notNull(tcOrder, () -> new BusinessException(TcOrderEnum.ErrorMsgEnum.ORDER_NOT_EXIST.getDesc()));
        return BeanUtil.copyProperties(tcOrder, TcOrderVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddOrders(List<TcOrderExcelData> orderList, int batchSize){
        if (CollectionUtil.isEmpty(orderList)){
            return;
        }
        List<TcOrder> entities = BeanUtil.copyToList(orderList, TcOrder.class);
        entities.forEach(item -> {
            item.setOrderId(UnqIdUtil.uniqueId());
            String pictureUrl = item.getPictureUrl();
            if (StrUtil.isNotBlank(pictureUrl)){
                String[] urls = pictureUrl.split(",");
                StringBuilder urlBuilder = new StringBuilder();
                for (int i = 0; i < urls.length; i++){
                    if (i > 0){
                        urlBuilder.append(",");
                    }
                    urlBuilder.append("http://59.57.7.8:8081/jjajxadmin-bucket/upload/upload_park/")
                            .append(urls[i].trim());
                }
                item.setPictureUrl(urlBuilder.toString());
            }

            // 检查车牌是否已被识别，如果未被识别则插入车牌表
            tcManager.checkAndInsertPlateNumber(item);
        });
        Db.executeBatch(entities, batchSize, TcOrderMapper.class, BaseMapper::insert);
    }

}
